package com.example.wuqujingdianphone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.api.ApiConfig;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.BookDetailBean;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * http://wenqujingdian.com/index.php/Jiekou/show/id/16725
 * 调取这个详情页接口，获取的数据更全面，不过这里为了减少网络请求的次数，增加用户体验
 * 直接从上一界面，传递过来了一些必要参数(不过目前为止，足以够用)
 */

public class BookDetailActivity extends BaseActivity {
    private static final String TAG = "BookDetailActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.btn_collect)
    Button mBtnCollect;
    @BindView(R.id.tv_mulu)
    TextView mTvMulu;
    private String bookId;


    @BindView(R.id.book_image)
    ImageView mBookImage;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_author)
    TextView mTvAuthor;
    @BindView(R.id.btn_read)
    Button mBtnRead;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.webView)
    WebView mWebView;

    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ButterKnife.bind(this);
        //BookDetailParcelable bookDetailBean = getIntent().getParcelableExtra("bookDetail");

        userName = SPUtil.getString(this, GlobalConfig.ACCOUNT_KEY, "");

        bookId = getIntent().getStringExtra("bookId");
        Log.i(TAG, "onCreate: bookId=" + bookId);

        String bookTitle = getIntent().getStringExtra("bookTitle");
        mTitle.setText(bookTitle);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getDataFromService();


        mBtnCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectBook();
            }
        });


    }

    private void collectBook() {
        //http://wenqujingdian.com/index.php/Jiekou/addsc/username/wwwwww/id/3428
        ViseHttp.GET("index.php/Jiekou/addsc/")
                .addParam("username", userName)
                .addParam("id", bookId)
                .tag(TAG)
                //.setHttpCache(true)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.i(TAG, "onSuccess: result= " + result);


                        if (result != null) {
                            if (result.contains("1")) {
                                ToastUtil.show("加入书架成功");
                            } else if (result.contains("0")) {
                                ToastUtil.show("您已经加入书架");
                            } else {
                                ToastUtil.show(result);
                            }
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        //请求失败，errCode为错误码，errMsg为错误描述
                        ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                    }
                });


    }

    private void getDataFromService() {
        // ((TextView) findViewById(R.id.adress)).setText(ApiConfig.APP_URL + "index.php/Jiekou/show/id/" + bookId);


        showLoading(this);

        ViseHttp.GET("index.php/Jiekou/show/id/" + bookId)
                .tag(TAG)
                .setHttpCache(true)
                .request(new ACallback<BookDetailBean>() {
                    @Override
                    public void onSuccess(BookDetailBean detailBean) {
                        cancelLoading();
                        bindData(detailBean);
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        cancelLoading();
                        //请求失败，errCode为错误码，errMsg为错误描述
                        ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                    }
                });


    }


    private void bindData(final BookDetailBean bookDetailBean) {

        Glide.with(this)
                .load(ApiConfig.APP_URL + bookDetailBean.spic)
                .error(R.drawable.icon_book_cover_defalt)
                .placeholder(R.drawable.icon_book_cover_defalt)
                .fitCenter()
                .into(mBookImage);

        mTvName.setText(bookDetailBean.news_tit);
        mTvAuthor.setText("作者：" + bookDetailBean.author);

        Log.i(TAG, "initView: bookId=" + bookDetailBean.id);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(ApiConfig.APP_URL + "index.php/Jiekou/getimg/id/" + bookDetailBean.id);
        mBtnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isLogin = SPUtil.getBoolean(BookDetailActivity.this, "isLogin", false);

                if (isLogin) {
                    Intent intent = new Intent(BookDetailActivity.this, BookReadActivity.class);
                    intent.putExtra("pdfurl", bookDetailBean.url);
                    startActivity(intent);
                } else {
                    ToastUtil.show("抱歉！您还没有登录哦！");
                }


            }
        });

        if (TextUtils.isEmpty(bookDetailBean.mulu)) {
            mTvMulu.setText("暂无目录");
        } else {
            mTvMulu.setText(bookDetailBean.mulu);
        }

     /*   mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        ShuPingAdapter shuPingAdapter = new ShuPingAdapter(null);
        mRecyclerView.setAdapter(shuPingAdapter);*/

    }


   /* private class ShuPingAdapter extends BaseQuickAdapter<BookListBean, BaseViewHolder> {

        public ShuPingAdapter(@Nullable List<BookListBean> data) {
            super(R.layout.item_shuping, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BookListBean item) {

        }
    }*/

}
